package view.GUI.adminView.coreData.people;

import model.data.interfaces.IFinishable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import enums.GUIText;
import view.GUI.adminView.coreData.AdminDataManaging;
import view.GUI.mainView.MainView;
import view.Validator.MyValidationSupport;


public abstract class AbstractPeopleView extends AdminDataManaging{
	
	public AbstractPeopleView(boolean isEdit, int rows, MainView instance, GUIText title) {
		super(isEdit, rows, instance, title);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	//cast is typesafe because only classes implementing IPerson are saved in a ComboBox
	protected void addNode(Node node, int column, int row){
		if(node instanceof TextField){
			((TextField) node).textProperty().addListener(new ChangeListener<String>(){

				@Override
				public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
					btnSave.setDisable(MyValidationSupport.isNodeToDisable(valSup));
				}
			});
		} else if(node instanceof ComboBox<?>){
			((ComboBox<IFinishable>) node).getSelectionModel().selectedItemProperty().addListener(new ChangeListener<IFinishable>(){

				@Override
				public void changed(ObservableValue<? extends IFinishable> observable, IFinishable oldValue, IFinishable newValue) {
					btnSave.setDisable(MyValidationSupport.isNodeToDisable(valSup));
				}
			});
		}else if(node instanceof Pane){
			for(Node n: ((Pane) node).getChildren()){
				if(n instanceof TextField){
					((TextField) n).textProperty().addListener(new ChangeListener<String>(){

						@Override
						public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
							btnSave.setDisable(MyValidationSupport.isNodeToDisable(valSup));
						}
					});
				}
			}
		}
		super.addNode(node, column, row);
	}


}
